package com.ny.controller;


import com.ny.entity.Classrooms;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.UserService;
import com.ny.service.impl.UserServicelmpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    //登录功能
    @Autowired
    private UserService userService;

    @Autowired
    private ClassroomService classroomService;
    //自动注入


    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        Users user = userService.login(username, password);
        session.setAttribute("user", user);
//        session.setAttribute("user", user);
        if (user != null && user.getRole().equals("学生")) {
            System.out.println(user);
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
            System.out.println(classroomsList);
            model.addAttribute("classroomList", classroomsList);
//            model.addAttribute("user", user);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }

    //该控制器方法使用 `@GetMapping` 注解，映射到 `/searchClassrooms` 的 GET 请求。
    @GetMapping("/searchClassrooms")
    public String searchClassrooms(
            @RequestParam(required = false) String classroomName,
            @RequestParam(required = false) String capacity,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String building,
            @RequestParam(required = false) String description,
    //`@RequestParam(required = false)`：表示这些请求参数不是必需的，若用户未输入对应条件，则参数值为 `null`。
            Model model) {
        List<Classrooms> classroomList = classroomService.searchClassrooms(classroomName, capacity, status, building, description);
        System.out.println("获取到的数据" + classroomList);
        model.addAttribute("classroomList", classroomList);
        return "classroom";//最后返回 `classroom` 页面展示结果。
    }
}
